package com.by.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.entity.Sku;
import com.by.entity.SpuSkuAttrValue;
import com.by.mapper.SkuMapper;
import com.by.service.SkuService;
import com.by.service.SpuSkuAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 江俊俊
 * @version 1.0
 * @description
 * @date 2025/4/10 21:27
 * @since JDK1.8
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SpuSkuAttrValueService spuSkuAttrValueService;

    @Override
    public List<Sku> getSkuBySpuId(Long spuId) {
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        List<Sku> skus = skuMapper.selectList(queryWrapper);

        //根据skuId查询sku销售属性关联表
        skus.forEach(sku -> {
            QueryWrapper<SpuSkuAttrValue> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("sku_id",sku.getSkuId());
            //根据skuId查询sku销售属性关联表
            List<SpuSkuAttrValue> spuSkuAttrValues = spuSkuAttrValueService.list(queryWrapper1);
            sku.setSpuSkuAttrValueList(spuSkuAttrValues);
        });

        return skus;
    }
}
